package com.product.miaosha.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.product.miaosha.vo.GoodsVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.product.miaosha.entity.MiaoshaActivityEntity;
import com.product.miaosha.service.MiaoshaActivityService;
import com.product.common.utils.PageUtils;
import com.product.common.utils.R;



/**
 * 
 *
 * @author brh
 * @email sunlightcs@gmail.com
 * @date 2022-02-22 16:55:10
 */
@RestController
@RequestMapping("miaosha/miaoshaactivity")
public class MiaoshaActivityController {
    @Autowired
    private MiaoshaActivityService miaoshaActivityService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = miaoshaActivityService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		MiaoshaActivityEntity miaoshaActivity = miaoshaActivityService.getById(id);

        return R.ok().put("miaoshaActivity", miaoshaActivity);
    }

    /**
     * 保存
    **/
    @RequestMapping("/save")
    public R save(@RequestBody MiaoshaActivityEntity miaoshaActivity){
		miaoshaActivityService.save(miaoshaActivity);
		//删除redis,重新加载redis里的数据
        redisTemplate.delete("goodsList");
		//添加秒杀商品时,把该商品的库存信息存入到redis中
        Integer stockCount = miaoshaActivity.getStockCount();
        redisTemplate.boundHashOps(miaoshaActivity.getGoodsId()).put("StockCount",stockCount);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MiaoshaActivityEntity miaoshaActivity){
		miaoshaActivityService.updateById(miaoshaActivity);
        //删除redis,重新加载redis里的数据
        redisTemplate.delete("goodsList");
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){

		miaoshaActivityService.removeByIds(Arrays.asList(ids));
        //删除redis,重新加载redis里的数据
        redisTemplate.delete("goodsList");
        return R.ok();
    }

    //商品信息
    @RequestMapping("/shopInfo/{id}")
    public GoodsVo shopInfo(@PathVariable("id") Integer id){
        GoodsVo shop = miaoshaActivityService.shopInfo(id);

        return shop;
    }

    @RequestMapping("/MiaoShaGoodsInfo")
    public MiaoshaActivityEntity MiaoShaGoodsInfo(@RequestParam Long gid){
        return miaoshaActivityService.MiaoShaGoodsInfo(gid);
    }


}
